<!--设置模块唯一ID-->
<div id="lzk_kucunbiao_add">
<!--标题、面包屑-->
<section class="content-header">
    <h1>
        库存申请
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li>绿植库</li>
        <li>库存表</li>
        <li class="active">库存申请</li>
    </ol>
</section>
<!--列表-->
<section class="content">

</section>
<!--添加、修改弹出层-->
<script type="text/template" data-for="tp_editor">
    <form role="form" class="form-horizontal">
        <div class="u-area">
            <div class="form-group">
                <label class="col-sm-2 control-label">编号：<%=data.p1?data.p1:''%></label>
                <div class="col-sm-4">
                    <input type="text" class="form-control"
                           value="<%=data.p1?data.p1:''%>" disabled>
                </div>

                <label class="col-sm-2 control-label">状态</label>
                <div class="col-sm-4">
                    <select class="form-control" name="p2">
                        <option>草稿</option>
                        <option>待审批</option>
                        <option>通过</option>
                        <option>不通过</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="u-box">
            <div class="u-box-header">
                <h3>操作人信息</h3>
            </div>
            <div class="u-area">
                <div class="form-group">
                    <label class="col-sm-2 control-label">操作人</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" name="p3"
                               value="<%=data.p3?data.p3:''%>">
                    </div>

                    <label class="col-sm-2 control-label">当前时间</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" name="p4"
                               value="<%=data.p4?data.p4:''%>">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">所属部门</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" name="p3"
                               value="<%=data.p3?data.p3:''%>">
                    </div>

                    <label class="col-sm-2 control-label">联系方式</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" name="p4"
                               value="<%=data.p4?data.p4:''%>">
                    </div>
                </div>
            </div>
        </div>
        <div class="u-box">
            <div class="u-box-header">
                <h3>入库信息</h3>
            </div>
            <div class="u-area">
                <div class="form-group">
                    <div class="col-sm-2"></div>
                    <div class="col-sm-10">
                        <table class="table table-bordered table-striped table-hover u-inline-table">
                            <thead>
                                <th>类别</th>
                                <th>名称</th>
                                <th>颜色</th>
                                <th>规格</th>
                                <th>单位</th>
                                <th>数量</th>
                                <th>单价</th>
                                <th>金额</th>
                                <th>批次</th>
                                <th>供应商</th>
                                <th>采购员</th>
                                <th>备注</th>
                                <th>操作</th>
                            </thead>
                            <tbody>

                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="border"></div>

                <div class="form-group">
                    <label class="col-sm-2 control-label">合计金额</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" name="p3"
                               value="<%=data.p3?data.p3:''%>">
                    </div>
                </div>
            </div>
        </div>

        <div class="box-footer">
            <button type="submit" class="btn btn-primary pull-right">确认</button>
        </div>
    </form>
</script>

<script>
    USEN(function (param) {
        //模块ID(全系统唯一)
        var tableId = "#lzk_kucunbiao";
        var id = "#lzk_kucunbiao_add";
        var that = this;

        //定义页面变量
        var $el = $(id);
        var $editor = $el.children('script[data-for=tp_editor]');

        $.ajax({
            url:"./simdata/common_add_action.json",
            success:function(data){
                $el.children(".content").html(function(){
                    return _.template($editor.html())({
                        data:data
                    })
                });

                var $table = $(id+' table.table tbody');
                var tr = '<tr>' +
                        '<td editor></td>' +
                        '<td editor></td>' +
                        '<td editor></td>' +
                        '<td editor></td>' +
                        '<td editor></td>' +
                        '<td editor></td>' +
                        '<td editor></td>' +
                        '<td editor></td>' +
                        '<td editor></td>' +
                        '<td editor gongyingshang></td>' +
                        '<td editor caigouyuan></td>' +
                        '<td editor></td>' +
                        '<td><a href="javascript:;" class="add"> <i class="fa fa-plus"></i></a><a href="javascript:;" class="reduce"> <i class="fa fa-minus"></i></a></td></tr>';

                $table.append(tr).on('click','td[editor]',function(){
                    if($(this).find('input').length!=0) return false;
                    var val = $(this).text();
                    $(this).html('<input type="text" value="' + val + '"/>');

                    $(this).siblings('td').each(function(){
                        $(this).html($(this).find("input").val())
                    });

                    var _config = {};
                    //供应商自动联想配置
                    if($(this).is('[gongyingshang]')){
                        _config.url = "./simdata/common_autocomplete_action.json?gongyingshang=1";
                        _config.choose = function(data){
                            //合并tr上绑定的数据
                            $.extend($input.closest('tr').data('cacheData'),{
                                gongyingshang : data['real-value']
                            });
                        }
                    }
                    //采购员自动联想配置
                    else if($(this).is('[caigouyuan]')){
                        _config.url = "./simdata/common_autocomplete_action.json?caigouyuan=1";
                        _config.choose = function(data){
                            //合并tr上绑定的数据
                            $.extend($input.closest('tr').data('cacheData'),{
                                caigouyuan : data['real-value']
                            });
                        }
                    }else{

                    }

                    var $input = $(this).find('input');
                    //合并配置
                    var _config = $.extend({
                        $el:$input,
                        url:"./simdata/common_autocomplete_action.json",
                        param:{
                            anyKey:"anyValue"
                        },
                        before:function(){
                            //动态增加自动联想必须加此行
                            $input.trigger('focus');
                        },
                        formatItem:function(item){
                            //显示结果
                            return item["name"]+'('+item["id"]+')';
                        },
                        setValue:function(item){
                            //选中结果
                            var obj = {"name":item["name"],"age":item["age"]}
                            //return {'data-value':item["name"],'real-value':item["id"]};
                            return {'data-value':item["name"],'real-value':obj};
                        },
                        choose:function(data){

                            //在tr上绑定要提交的数据
                            $.extend($input.closest('tr').data('cacheData'),{
                                name : data['data-value'],
                                id : data['real-value']
                            });

                            //为商品信息相关单元格填值
                            $input.closest('tr').find('td[editor]').each(function(i,j){
                                //跳过采购员
                                if($(this).index() == 10){
                                    return
                                }else if($(this).is(':odd')){
                                    $(this).html(data['data-value']);
                                }else{
                                    $(this).html(data['real-value']);
                                }
                            });

                        }
                    },_config);
                    //初始化自动联想
                    that.autocomplete(_config);
                }).on('blur','input',function(){
                    var _t = $(this);
                    setTimeout(function() {
                        _t.parent('td').html(_t.val());
                    },300)
                }).on('click','a.add',function(){
                    var $tr = $(tr);
                    $tr.data('cacheData',{});
                    $table.append($tr);
                }).on('click','a.reduce',function(){
                    if($table.find("tr").length > 1)
                        $(this).closest('tr').remove();
                }).find('tr:first').data('cacheData',{});

                //提交表单
                $el.find("form").ajaxForm({
                    url: './simdata/common_add_action.json',
                    type:'post',
                    data:{
                        //收集table数据,传给服务器
                        tableData : function(){
                            var tableData = [];
                            $table.find('tr').each(function(){
                                var cache = $(this).data('cacheData');
                                if(cache){
                                    tableData.push(cache);
                                }
                            });
                            return JSON.stringify(tableData);
                        }
                    },
                    success: function (data, state, obj) {
                        USEN.notifyAlert({
                            title: "提示:",
                            content: '操作成功,刷新列表'
                        });
                        USEN.getDatatable(tableId + ' table.table').ajax.reload();
                        USEN.closeActiveTab();
                    }
                });

                //初始化select插件
                that.select2({
                    $el:$el.find("select"),
                    dropdownParent:$el
                });
            }
        })

    });
</script>
</div>